package com.neusoft.elmboot.service.impl;

import java.util.*;

import com.neusoft.elmboot.mapper.CartMapper;
import com.neusoft.elmboot.po.Cart;
import com.neusoft.elmboot.po.ClassifyElement;
import com.neusoft.elmboot.util.APIResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neusoft.elmboot.mapper.FoodMapper;
import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.service.FoodService;

@Service
public class FoodServiceImpl implements FoodService {

	@Autowired
	private FoodMapper foodMapper;

	@Autowired
	private CartMapper cartMapper;



	@Override
	public APIResponse listFoodByBusinessIdAndUserId(Integer businessId, String userId) {
		//返回内容是一个list
		//list的内容是分类
		//有个分类id
		//每个分类里面有个数组
		//数组里面有food
		//food要携带上数量和价格
		List<ClassifyElement> ret = new ArrayList<>();
		List<Food> foods = foodMapper.listFoodByBusinessId(businessId,userId);
		Map<Integer,List<Food>> map = new HashMap<>();
		for(Food food: foods){
			Integer key = food.getClassifyId();
			if(map.containsKey(key)){
				map.get(key).add(food);
			}else{
				List<Food> list = new ArrayList<>();
				list.add(food);
				map.put(key,list);
			}
		}

		for(Map.Entry<Integer,List<Food>> e: map.entrySet()){
			ClassifyElement classifyElement = new ClassifyElement(e.getKey(), e.getValue());
			classifyElement.setClassifyName(e.getValue().get(0).getClassifyName());
			ret.add(classifyElement);
		}
		return  APIResponse.success(ret);

	}

}
